package com.ywl.gulimall.ware.service.impl;

import com.ywl.common.constants.PurchaseConstant;
import com.ywl.gulimall.ware.entity.PurchaseDetailEntity;
import com.ywl.gulimall.ware.service.PurchaseDetailService;
import com.ywl.gulimall.ware.vo.MergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.common.utils.PageUtils;
import com.ywl.common.utils.Query;

import com.ywl.gulimall.ware.dao.PurchaseDao;
import com.ywl.gulimall.ware.entity.PurchaseEntity;
import com.ywl.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceiveList(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", 0).or().eq("status", 1)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void savePurchase(MergeVo mergeVo) {
        // 设置purchaseId
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchase = new PurchaseEntity();
            purchase.setStatus(PurchaseConstant.PurchaseStatus.NEW.getCode());
            purchase.setCreateTime(new Date());
            purchase.setUpdateTime(new Date());
            this.save(purchase);
            purchaseId = purchase.getId();
        }
        // 更新purchaseDetail
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = mergeVo.getItems().stream().map(i -> {
            PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
            purchaseDetail.setId(i);
            purchaseDetail.setPurchaseId(finalPurchaseId);
            purchaseDetail.setStatus(PurchaseConstant.PurchaseDetailConstant.ASSIGNED.getCode());
            return purchaseDetail;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        // 更新purchase updatetime
        PurchaseEntity purchase = new PurchaseEntity();
        purchase.setStatus(PurchaseConstant.PurchaseStatus.ASSIGNED.getCode());
        purchase.setUpdateTime(new Date());
        purchase.setId(purchaseId);
        this.updateById(purchase);
    }

}